package com.ziven.common.widget.window;

import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.ziven.common.widget.R;
import com.ziven.common.widget.setting.PreferenceFragment;
import com.ziven.common.widget.title.TitleView;
import com.ziven.common.widget.title.TitleEffect;
import com.ziven.common.widget.value.Title;
import com.ziven.common.widget.value.Value;
import com.ziven.common.widget.value.Values;
import com.ziven.common.widget.value.Visibility;
import com.ziven.easygo.EasyGos;
import com.ziven.easygo.autowired.EasyGo;
import com.ziven.easygo.ui.AbstractBaseActivity;
import com.ziven.easygo.util.EasyUtils;
import com.ziven.easygo.util.Nulls;
import com.ziven.easygo.util.ResourceUtils;

/**
 * @author Ziven
 */
public class FragmentContainerActivity extends AbstractBaseActivity implements TitleEffect {

    public static final String PATH = "com.ziven.common.widget.window.FragmentContainerActivity";
    private static final String FRAGMENT_NAME_KEY = "FRAGMENT_NAME_KEY";
    private static final String ACTIVITY_TITLE_KEY = "ACTIVITY_TITLE_KEY";
    private static final String ACTIVITY_CONTAINER_KEY = "ACTIVITY_CONTAINER_KEY";
    private static final String NO_TITLE_KEY = "NO_TITLE_KEY";
    private static final Title NO_TITLE = Title.title(NO_TITLE_KEY);
    private Value titleValue;

    public static void startPreferenceFragmentActivity(@NonNull String key) {
        startFragmentContainerActivity(PreferenceFragment.class.getName(), Nulls.of(NO_TITLE), key);
    }

    public static void startPreferenceFragmentActivity(@NonNull Nulls<Title> title,
                                                       @NonNull String key) {
        startFragmentContainerActivity(PreferenceFragment.class.getName(), title, key);
    }

    public static void startFragmentContainerActivity(@NonNull String fragmentName) {
        startFragmentContainerActivity(fragmentName, Nulls.of(NO_TITLE), null);
    }

    public static void startFragmentContainerActivity(@NonNull String fragmentName,
                                                      @NonNull Nulls<Title> title) {
        startFragmentContainerActivity(fragmentName, title, null);
    }

    public static void startFragmentContainerActivity(@NonNull String fragmentName,
                                                      @NonNull String key) {
        startFragmentContainerActivity(fragmentName, Nulls.of(NO_TITLE), key);
    }

    public static void startFragmentContainerActivity(@NonNull String fragmentName,
                                                      @NonNull Nulls<Title> title,
                                                      @Nullable String key) {
        EasyGo.easyGo(FragmentContainerActivity.class, intent -> {
            intent.putExtra(FRAGMENT_NAME_KEY, fragmentName);
            title.doNotNull(value -> {
                if(value.resId != 0) {
                    intent.putExtra(ACTIVITY_TITLE_KEY, ResourceUtils.getText(value.resId));
                } else {
                    value.value(String.class, value2 -> intent.putExtra(ACTIVITY_TITLE_KEY, value2));
                }
            });
            intent.putExtra(ACTIVITY_CONTAINER_KEY, key);
        });
    }

    @Override
    protected Object obtainLayout() {
        return R.layout.activity_fragment_container;
    }

    @Override
    protected void initLayout() {
        Fragment fragment;
        String containerKey = getIntent().getStringExtra(ACTIVITY_CONTAINER_KEY);
        if((fragment = EasyUtils.newInstance(getIntent().getStringExtra(FRAGMENT_NAME_KEY))) != null) {
            if(fragment instanceof PreferenceFragment) {
                ((PreferenceFragment) fragment).setPreferenceItemsFactoryId(containerKey);
            }
            getSupportFragmentManager()
                    .beginTransaction()
                    .add(R.id.container, fragment)
                    .commitNow();
        }
        initTitleValue(containerKey);

        String title = getIntent().getStringExtra(ACTIVITY_TITLE_KEY);
        if(TextUtils.equals(NO_TITLE_KEY, title)) {
            initEffect(Visibility.gone());
        } else {
            initEffect(Title.title(title == null ? "" : title));
        }
    }

    private void initTitleValue(@Nullable String containerKey) {
        EasyGos.getEasyGo().easyGo(
                EasyGo.Transition.of(PATH)
                        .param(TextUtils.isEmpty(containerKey) ? PATH : containerKey)
                        .carry(value -> {
                            if(value instanceof Value) {
                                titleValue = (Value) value;
                            }
                        }));
    }

    @Override
    protected void destroyLayout() {

    }

    @NonNull
    @Override
    public TitleView obtainEffectView() {
        return getView(R.id.title);
    }

    @NonNull
    @Override
    public Value obtainValue() {
        return titleValue != null
                ? Values.mergeRemove(TitleEffect.super.obtainValue(), titleValue)
                : TitleEffect.super.obtainValue();
    }
}
